package com.journey.org.ui

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.KeyEvent
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import com.journey.org.BR
import com.journey.org.R
import com.journey.org.databinding.ActivityMainBinding
import com.journey.org.ui.home.HomeFragment
import com.journey.org.ui.knowledge.KnowLedgeFragment
import com.journey.org.ui.mine.MineFragment
import com.journey.org.ui.video.VideoFragment
import com.mvvmhabit.org.base.AppManager
import com.mvvmhabit.org.base.BaseActivity
import com.mvvmhabit.org.base.BaseViewModel
import com.mvvmhabit.org.utils.ToastUtils
import me.majiajie.pagerbottomtabstrip.NavigationController
import me.majiajie.pagerbottomtabstrip.listener.OnTabItemSelectedListener

/**
 * @Date: 2020/11/17 11:56
 * @Author:小妖
 * @Description:主界面
 */
class MainActivity : BaseActivity<ActivityMainBinding, BaseViewModel>() {
    // Fragment 集合
    private val mFragments = ArrayList<Fragment>()

    //退出时的时间
    private var mExitTime: Long = 0

    // 底部Tab控制器
    private var navigationController: NavigationController? = null

    // 选择办公场所之前的 tab 的下标
    private var oldTabIndex = 0

    override fun layoutId(savedInstanceState: Bundle?): Int = R.layout.activity_main

    override fun initVariableId(): Int = BR.viewModel

    override fun initViewModel(): Class<BaseViewModel> = BaseViewModel::class.java

    override fun initData() {
        super.initData()
        mFragments.add(HomeFragment())
        mFragments.add(VideoFragment())
        mFragments.add(KnowLedgeFragment())
        mFragments.add(MineFragment())
        commitAllowingStateLoss(0)
        initBottomTab()
    }

    private fun initBottomTab() {
        val item = mBinding.mainBottomTab.material()
        item.addItem(R.mipmap.ic_home_icon, "首页")
        item.addItem(R.mipmap.ic_home_recreation_icon, "娱乐")
        item.addItem(R.mipmap.ic_home_knowledge_icon, "知识")
        item.addItem(R.mipmap.ic_home_personal_icon, "个人中心")
        item.setDefaultColor(ContextCompat.getColor(this, R.color.gray))
        navigationController = item.build()
        // 设置学习有圆点信息
        navigationController?.setMessageNumber(3, 20)
        // 设置有消息
        navigationController?.setHasMessage(2, true)
        navigationController?.addTabItemSelectedListener(object : OnTabItemSelectedListener {
            override fun onSelected(index: Int, old: Int) {
                oldTabIndex = index
                commitAllowingStateLoss(index)
            }

            override fun onRepeat(index: Int) {
            }
        })
    }

    private fun commitAllowingStateLoss(index: Int) {
        hideAllFragment()
        val manager = supportFragmentManager.beginTransaction()
        var fragment = supportFragmentManager.findFragmentByTag(index.toString())
        if (fragment != null) {
            manager.attach(fragment)
        } else {
            fragment = mFragments[index]
            manager.add(R.id.main_content, fragment, index.toString())
        }
        manager.commitAllowingStateLoss()
    }

    private fun hideAllFragment() {
        val manager = supportFragmentManager.beginTransaction()
        for (i in mFragments.indices) {
            val currentFragment =
                supportFragmentManager.findFragmentByTag(i.toString())
            if (currentFragment != null) {
                manager.detach(currentFragment)
            }
        }
        manager.commitAllowingStateLoss()
    }

    //对返回键进行监听
    @SuppressLint("ShowToast")
    override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.repeatCount == 0) {
            if (System.currentTimeMillis() - mExitTime > 2000) {
                ToastUtils.showShort("再按一次退出程序")
                mExitTime = System.currentTimeMillis()
            } else {
                AppManager.getAppManager()?.finishAllActivity()
                finish()
            }
            return true
        }
        return super.onKeyDown(keyCode, event)
    }
}
